<?php

/**
 * Created on Jun 30, 2007
 * API for MediaWiki 1.8+
 *
 * Copyright © 2007 Roan Kattouw <Firstname>.<Lastname>@home.nl
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 * http://www.gnu.org/copyleft/gpl.html
 */
 
require_once( '/home/cyberpower678/public_html/bash/base.php' );
WebTool::setMemLimit();

/**
 * API module that facilitates deleting pages. The API eqivalent of action=delete.
 * Requires API write mode to be enabled.
 *
 * @ingroup API
 */
class ApiBash extends ApiBase {

        public function __construct( $main, $action ) {
                parent::__construct( $main, $action );
        
        }

        /**
         * Extracts the title, token, and reason from the request parameters and invokes
         * the local delete() function with these as arguments. It does not make use of
         * the delete function specified by Article.php. If the deletion succeeds, the
         * details of the article deleted and the reason for deletion are added to the
         * result object.
         */
        public function execute() {
                global $lang, $wiki, $url, $site;
                
                $params = $this->extractRequestParams();
                
                if ( !isset( $params['type'] ) ) {
                        $this->dieUsageMsg( array( 'missingparam', 'type' ) );
                }
                
                $ret = WebTool::loadForApi( 'Bash', false, false );
                
                $base = new BashBase( true );
                
                switch( $params['type'] ) {
                        case 'random':
                                $quote = $base->getRandomQuote();
                                
                                if( isset( $quote['error'] ) ) $this->dieUsage( $quote['info'], $quote['error'] );
                                
                                $data = array( array( 'id' => $quote['id'], '*' => $quote['quote'] ) );
                                break;
                        case 'showall':
                                $quotes = $base->getAllQuotes();
                                
                                $data = $quotes;
                                foreach( $data as $id => $quote ) {
                                        $data[$id] = array( '*' => $quote, 'id' => $id );
                                }
                                break;
                        case 'showone':
                                $quote = $base->getQuoteFromId( $params['id'] );
                                
                                if( isset( $quote['error'] ) ) $this->dieUsage( $quote['info'], $quote['error'] );
                                
                                $data = array( array( 'id' => $quote['id'], '*' => $quote['quote'] ) );
                                break;
                        case 'search':
                                $quotes = $base->getQuotesFromSearch( $params['search'] );
                                
                                $data = $quotes;
                                foreach( $data as $id => $quote ) {
                                        $data[$id] = array( '*' => $quote, 'id' => $id );
                                }
                                break;
                }
                
                $this->getResult()->setIndexedTagName( $data, 'quote' );
                $this->getResult()->addValue( 'query', $this->getModuleName(), $data );
        }

        public function getAllowedParams() {
                return array(
                        'type' => array(
                                ApiBase::PARAM_TYPE => array(
                                        'random',
                                        'showall',
                                        'showone',
                                        'search',
                                )
                        ),
                        'id' => 'integer',
                        'search' => null
                );
        }

        public function getParamDescription() {
                $p = $this->getModulePrefix();
                return array(
                        'type' => "Type of bash request to do",
                        'id' => 'When used with &type=showone, gets this quote ID',
                        'search' => 'When used with &type=search, searches for this string',
                );
        }

        public function getDescription() {
                return 'Get various quotes from [[m:BASH]]';
        }

        public function getPossibleErrors() {
                return array_merge( parent::getPossibleErrors(), array(
                        array( 'noquote' ),
                ) );
        }

        protected function getExamples() {
                return array(
                        'api.php?action=bash&type=random',
                        'api.php?action=bash&type=showall',
                        'api.php?action=bash&type=showone&id=178',
                        'api.php?action=bash&type=search&search=brion'
                );
        }

        public function getVersion() {
        }
}
